package com.RHECmap.com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class ElephantProfile extends Activity {

	private Button profile_information;
	private Button show_history;
	private Button whether_information;
	private String final_whether_string = "";
	private String img_url="";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.elephantprofile_view);	
		
		profile_information = (Button) findViewById(R.id.button3);
		show_history = (Button) findViewById(R.id.button1);
		whether_information = (Button) findViewById(R.id.button2);
		
		whether_information.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View arg0) {			
				
				if(final_whether_string == ""){
					set_whether_information();				
				}
				show_whether_dialog();
			}
		});
		
		show_history.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), selfMap.class);				
	            startActivityForResult(intent,0);      
				
			}
		});
		
	//	Toast.makeText( this,Data.Device_ID,Toast.LENGTH_SHORT ).show();	
	//	Toast.makeText( this,Data.lat,Toast.LENGTH_SHORT ).show();	
	//	Toast.makeText( this,Data.lon,Toast.LENGTH_SHORT ).show();	
	}
	
	
	@Override
	public void onBackPressed() {		
		super.onBackPressed();
		finish();
	}
 

	public void set_whether_information(){
		
    //	String lat = Integer.valueOf(Data.lat);
   // 	int lon = Integer.valueOf(Data.lon);
   // 	Toast.makeText( this,Data.lon,Toast.LENGTH_SHORT ).show();
    URL whether = null;
    try {
    	whether = new URL("http://www.google.com/ig/api?weather=,,,"+Data.lat+","+Data.lon+"");
    	
	} catch (MalformedURLException e) {					
		e.printStackTrace();
		
	}
    
    BufferedReader in = null;
	try {
		in = new BufferedReader(new InputStreamReader(whether.openStream()));
		String inputLine = "";	   
		String s;
		inputLine = in.readLine();		
		String data[] = inputLine.split("<");		
		int i = 0;
		while(i<data.length){
			if(data[i].contains("/current_conditions")){
				break;		
			}
			else if(data[i].contains("condition data=\"") && !data[i].contains("wind_condition data=\"")){
				final_whether_string += "Condition : " + find_infoemation(data[i]) + "\n";				
			}
			else if(data[i].contains("temp_f data=\"")){
				final_whether_string += "Fahrenheit : " +  find_infoemation(data[i]) + "F" +  "\n";
			} 
			else if(data[i].contains("temp_c data=\"")){
				final_whether_string += "Celsius   : " +  find_infoemation(data[i]) + "C" +  "\n";
			}
			else if(data[i].contains("humidity data=\"")){
				final_whether_string += find_infoemation(data[i]) + "\n";
			}
			else if(data[i].contains("icon data=\"")){
				img_url = "http://www.google.com" + find_infoemation(data[i]);
			
			}
			else if(data[i].contains("wind_condition data=\"")){
				final_whether_string += find_infoemation(data[i]) + "\n";
			}
			i++;

		}
	}
	catch (IOException e) {					
	e.printStackTrace();
	}
}
	
	
	
	private String find_infoemation(String s){
		String s_second_part = null;
		String s_second_part_first_part = null;
		String l[] = s.split("=\"");
		if(l.length>0){
			s_second_part = l[1];
			String l2[] = s_second_part.split("\"/>");
			if(l2.length>0){
				s_second_part_first_part = l2[0];
			}
		}
		return s_second_part_first_part;
	}
	
	
	public void show_whether_dialog(){
		final AlertDialog whetherDialog;
		whetherDialog = new AlertDialog.Builder(this).create();
		whetherDialog.setTitle("Current Whether State:");    		
		whetherDialog.setMessage(final_whether_string);
		Drawable drawable = null;
		if(img_url != ""){
	 	 drawable = get_whether_image(img_url);
		}
		whetherDialog.setIcon(drawable);
		whetherDialog.setButton("OK" ,new OnClickListener() {
			
			public void onClick(DialogInterface arg0, int arg1) {
				whetherDialog.dismiss();				
			}
		});
		whetherDialog.show(); 
	}
	
	
	public Drawable get_whether_image(String img){
		URL url = null;
		try {
			url = new URL(img);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		InputStream content = null;
		try {
			content = (InputStream)url.getContent();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return  Drawable.createFromStream(content , "src"); 
		
		}
	}
	